const state = {
  curTarget: {  // 当前编辑 item; ⚠️注意：仅暂存 itemId 以及 parentId
    itemId: '',
    parentId: '',
  },
  curPanel: -1,  // 当前弹层 panels[0]
  curPanelData: null,  // 当前弹层 data
};

const getters = {};

const actions = {
  updateCurPanel({commit, rootState}, params) {
    commit('updateCurPanel', {rootState: rootState, index: params});
  },
  updatePanelLists({commit, dispatch, state}, params) {
    dispatch('cardComponents/updatePanelLists', {data: params, index: state.curPanel}, {root: true});
    commit('updatePanelLists', params);
  },
  updateCurTarget({commit}, params) {
    commit('updateCurTarget', params);
  },
};

const mutations = {
  updateCurPanel(state, {rootState, index}) {
    // 1. update curPanel
    state.curPanel = index;

    // 2. update cuePageData
    state.curPanelData = index > -1 ? rootState.cardComponents.siteData.panels[index] : null;
  },
  updatePanelLists(state, data) {
    state.curPanelData = data;
  },
  updateCurTarget(state, data) {
    state.curTarget = data;
  },

  /* beforeUnmount */
  clear(state, data) {
    // clear panelComponents
    state.curTarget = {
      itemId: '',
      parentId: '',
    };
    state.curPanel = -1;
    state.curPanelData = null;
  },
};

export default {
  namespaced: true,
  state: state,
  getters: getters,
  actions: actions,
  mutations: mutations,
}
